﻿
using Qiniu.Common;
using Qiniu.Http;
using Qiniu.IO;
using Qiniu.IO.Model;
using Qiniu.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JMcms.Qiniu
{
   public class UploadQiniu
    {
        /// <summary>
        /// 简单上传-上传字节数据
        /// </summary>
        public void UploadData()
        {
            // 生成(上传)凭证时需要使用此Mac
            // 这个示例单独使用了一个Settings类，其中包含AccessKey和SecretKey
            // 实际应用中，请自行设置您的AccessKey和SecretKey
            //Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);
            Mac mac = new Mac("utisb-4JAEjcy9aRWNVDHRjXOejQyTeq1yx4JOOD", "7x2tYPhFt_zGNUsQefvQJznz2yr_dU5-QkCN5fQZ");

            string bucket = "test";
            string saveKey = "myfile";
            byte[] data = System.IO.File.ReadAllBytes("D:/QFL/1.mp3");
            //byte[] data = System.Text.Encoding.UTF8.GetBytes("Hello World!");

            // 上传策略，参见 
            // https://developer.qiniu.com/kodo/manual/put-policy
            PutPolicy putPolicy = new PutPolicy();

            // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖)，请使用 SCOPE = "BUCKET:KEY"
            // putPolicy.Scope = bucket + ":" + saveKey;
            putPolicy.Scope = bucket;

            // 上传策略有效期(对应于生成的凭证的有效期)          
            putPolicy.SetExpires(3600);

            // 上传到云端多少天后自动删除该文件，如果不设置（即保持默认默认）则不删除
           // putPolicy.DeleteAfterDays = 1;

            // 生成上传凭证，参见
            // https://developer.qiniu.com/kodo/manual/upload-token            

            string jstr = putPolicy.ToJsonString();
            string token = Auth.CreateUploadToken(mac, jstr);

            FormUploader fu = new FormUploader();

            HttpResult result = fu.UploadData(data, saveKey, token);

            Console.WriteLine(result);
        }
    }
}
